<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="openTransfer" :loading="loading"
             @close="handleClose" @open="handleOpen" width="400px" append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="用户" prop="userName">
        <el-select v-model="form.userName" @change="handleChange" filterable placeholder="请选择员工">
          <el-option v-for="user in userList" :key="user.userId" :label="user.userName" :value="user.userId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  userSelect
} from '@/api/printer/user'

export default {
  name: 'UserSelectComponent',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: '选择用户',
      // 是否显示弹出层
      openTransfer: false,
      userList: undefined,
      // 查询参数
      queryParams: {},
      // 表单参数
      form: { userId: undefined, phone: undefined },
      // 返回参数
      returnParams: {
        userId: undefined,
        userName: undefined
      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    /** 查询用户列表 */
    getUserList() {
      this.loading = true
      userSelect(this.queryParams).then((response) => {
        this.userList = response.data
        this.loading = false
      })
    },
    open(userId) {
      this.form.userId = userId
      this.openTransfer = true
    },
    reset() {

    },
    handleOpen() {

    },
    handleClose() {
      this.form = {
        userId: undefined,
        phone: undefined
      }
      this.openTransfer = false
      this.$emit('close-dialog', )
    },
    handleChange(e) {
      this.returnParams = this.userList.find(function(item){
        return item.userId === e;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.openTransfer = false;
          this.$emit('close-dialog',this.returnParams);
        }
      })
    }
  }
}
</script>
